/*=========================================================================

  Library:   CFS

  Copyright (c) Kitware Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=========================================================================*/

// Qt includes
#include <QFile>
#include <QTextStream>

// CFS includes
#include "CFSFileLogger.h"

// --------------------------------------------------------------------------
// CFSFileLoggerPrivate

// --------------------------------------------------------------------------
class CFSFileLoggerPrivate
{
  Q_DECLARE_PUBLIC(CFSFileLogger);
protected:
  CFSFileLogger* const q_ptr;
public:
  CFSFileLoggerPrivate(CFSFileLogger& object);
  ~CFSFileLoggerPrivate();

  void init();

  bool Enabled;
  QString FilePath;
  int NumberOfFilesToKeep;
};

// --------------------------------------------------------------------------
CFSFileLoggerPrivate::CFSFileLoggerPrivate(CFSFileLogger& object)
  : q_ptr(&object)
{
  this->Enabled = true;
  this->NumberOfFilesToKeep = 10;
}

// --------------------------------------------------------------------------
CFSFileLoggerPrivate::~CFSFileLoggerPrivate()
{
}

// --------------------------------------------------------------------------
void CFSFileLoggerPrivate::init()
{
}

// --------------------------------------------------------------------------
// CFSFileLogger

// --------------------------------------------------------------------------
CFSFileLogger::CFSFileLogger(QObject* parentObject)
  : Superclass(parentObject)
  , d_ptr(new CFSFileLoggerPrivate(*this))
{
  Q_D(CFSFileLogger);
  d->init();
}

// --------------------------------------------------------------------------
CFSFileLogger::~CFSFileLogger()
{
}

// --------------------------------------------------------------------------
bool CFSFileLogger::enabled()const
{
  Q_D(const CFSFileLogger);
  return d->Enabled;
}

// --------------------------------------------------------------------------
void CFSFileLogger::setEnabled(bool value)
{
  Q_D(CFSFileLogger);
  d->Enabled = value;
}

// --------------------------------------------------------------------------
QString CFSFileLogger::filePath()const
{
  Q_D(const CFSFileLogger);
  return d->FilePath;
}

// --------------------------------------------------------------------------
void CFSFileLogger::setFilePath(const QString& filePath)
{
  Q_D(CFSFileLogger);
  d->FilePath = filePath;
}

// --------------------------------------------------------------------------
int CFSFileLogger::numberOfFilesToKeep()const
{
  Q_D(const CFSFileLogger);
  return d->NumberOfFilesToKeep;
}

// --------------------------------------------------------------------------
void CFSFileLogger::setNumberOfFilesToKeep(int value)
{
  Q_D(CFSFileLogger);
  d->NumberOfFilesToKeep = value;
}

// --------------------------------------------------------------------------
void CFSFileLogger::logMessage(const QString& msg)
{
  Q_D(CFSFileLogger);
  if (!d->Enabled)
    {
    return;
    }
  QFile f(d->FilePath);
  if (!f.open(QFile::Append))
    {
    return;
    }
  QTextStream s(&f);
  s << msg << endl;
  f.close();
}

